package a.a;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;

public class VideoActivity extends Activity implements OnPreparedListener, OnVideoSizeChangedListener {
    ScreenView v;
    MediaPlayer m;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 0, "start");
        menu.add(0, 2, 0, "pause");
        menu.add(0, 3, 0, "stop");
        menu.add(0, 4, 0, "reset");
        menu.add(0, 5, 0, "release");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            if (v.mHolder != null) {
                play(v.mHolder);
            }
            break;
        case 2:
            m.pause();
            break;
        case 3:
            m.stop();
            break;
        case 4:
            m.reset();
            break;
        case 5:
            m.release();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        m = new MediaPlayer();
        v = new ScreenView(this);
        WindowManager win = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        setContentView(v);
    }
    private void play(SurfaceHolder h) {
        m.setDisplay(h);
        m.setScreenOnWhilePlaying(true);
        m.setOnPreparedListener(this);
        m.setOnVideoSizeChangedListener(this);
        try {
            m.setDataSource(Environment.getExternalStorageDirectory() + "/" + "2NE1.mp4");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        m.prepareAsync();
    }

    class ScreenView extends SurfaceView implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        int w;
        int h;

        public ScreenView(Context context) {
            super(context);
            setBackgroundColor(Color.BLUE);
            getHolder().addCallback(this);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Log.e("VideoActivity", "onMeasure()");
            if (w == 0 || h == 0) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            } else {
                setMeasuredDimension(w, h);
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.e("VideoActivity", "surfaceCreated()");
            mHolder = holder;
            play(holder);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.e("VideoActivity", "surfaceChanged()");
            mHolder = holder;
            w = width;
            h = height;
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.e("VideoActivity", "surfaceDestroyed()");
            holder = null;
            m.release();
        }
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mediaplayer, int w, int h) {
        Log.e("VideoActivity", "onVideoSizeChanged()");
    }
    @Override
    public void onPrepared(MediaPlayer mediaplayer) {
        Log.e("VideoActivity", "onPrepared()");
        v.getHolder().setFixedSize(mediaplayer.getVideoWidth(), mediaplayer.getVideoHeight());
        mediaplayer.start();
    }

}